﻿#include "systemconfiguration.h"
#include <QCoreApplication>
#include "cfgjsonrw.h"

#include "common_log.h"

static SystemConfiguration* instance = nullptr;
SystemConfiguration* SystemConfiguration::GetInstance()
{
	if (instance == nullptr) {
		instance = new SystemConfiguration();
	}

	return instance;
}

SystemConfiguration::SystemConfiguration()

{}

SystemConfiguration::~SystemConfiguration()
{}


bool SystemConfiguration::Init(const QString& path)
{
	m_cfg = new CfgJsonRW();
	if (!m_cfg->Init()) {
		log_error("Read From cfg file failed");
		return false;
	}

	return true;
}

/*获取项目名称*/
QString SystemConfiguration::GetProjectName()
{
	return m_cfg->GetProjectName();
}
/*机械手配置*/
RobotSetting SystemConfiguration::GetRobotSetting()
{
	auto cfg = m_cfg->GetRobotCfg();
	return TransFromRobotJ(cfg);
}
/*视觉配置*/
CameraSetting SystemConfiguration::GetCameraSetting(const QString& camId)
{
	CameraSetting set = {};
	auto all = m_cfg->GetCameraCfg();
	for (auto& iter : all.cameras()) {
		if (iter.id() == camId) {
			set.id = camId;
			set.alg = iter.alg();
			set.biaoding = iter.bd();
			set.ip = iter.ip();
			set.type = iter.type();
			set.picPath = all.picpath();
			set.visPath = all.visPath();
			break;
		}
	}
	return set;
}

QList<JigSetting> SystemConfiguration::GetJigSetting()
{
	auto cfg = m_cfg->GetJigCfg();
	JigSetting setting = TransFormJigCfg(cfg);
	QList<JigSetting> list;
	list.append(setting);
	return list;
}

/*WCS及机械手服务配置*/
WCSSetting SystemConfiguration::GetWcsCfg()
{
	return TransFromWCScfgJ(m_cfg->GetWCSCfg());
}

/*数据库路径*/
QString SystemConfiguration::GetSqlPath()
{
	return QCoreApplication::applicationDirPath() + "/config/robot.db";
}

/*业务线编号*/
QStringList SystemConfiguration::GetPipelineids()
{
	return m_cfg->GetAllPipelineID();
}
/*根据业务线编号获取业务线配置*/
PipelineSetting SystemConfiguration::GetPipelineCfgById(const QString& id)
{
	return TransFormPipelineCfg(m_cfg->GetPipelineCfgByID(id));
}
/*根据业务线编号获取业务线路径编号列表*/
QStringList SystemConfiguration::GetPipelinePathids(const QString& pipe)
{
	return m_cfg->GetAllPipelinePathID(pipe);
}
/*根据业务线编号获取业务线路径配置*/
PathSetting SystemConfiguration::GetPipelinePathCfgById(const QString& pipe, const QString& path)
{
	return TransFromPathCfg(m_cfg->GetPipelinePathCfgByID(pipe, path));
}

/*获取托盘编号列表*/
QStringList SystemConfiguration::GetStackPlateIds()
{
	return m_cfg->GetAllStackPlateID();
}

/*根据托盘编号获取托盘配置*/
StackPlateSetting SystemConfiguration::GetStackPlateCfg(const QString& id)
{
	return TransFormStackPlateCfg(m_cfg->GetStackPlateCfgByID(id));
}

/*获取托盘垛配置*/
PlateStackSetting SystemConfiguration::GetPlateStackCfg()
{
	PlateStackSetting out = {};
	auto cfg = m_cfg->GetPlateStackCfg();
	out.valid = cfg.valied() == 1 ? true : false;
	out.id = cfg.id();
	out.plateH = cfg.ph();
	out.base.TransFromPositonJ(cfg.base());
	out.maxDi = cfg.maxDi();
	out.minDi = cfg.minDi();
	out.rob = cfg.rob();

	return out;
}

/*获取输送线位置编号列表*/
QStringList SystemConfiguration::GetBoxlineIds()
{
	return m_cfg->GetAllBoxlineID();
}
/*根据输送线位置编号获取位置配置*/
BoxlineSetting SystemConfiguration::GetBoxlineCfg(const QString& id)
{
	return TransFromBoxlineCfg(m_cfg->GetBoxlineCfgByID(id));
}

/*获取拆垛托盘位置编号列表*/
QStringList SystemConfiguration::GetUnstackplateIds()
{
	return m_cfg->GetAllUnstackPlateID();
}
/*根据托盘编号获取拆垛托盘配置*/
UnstackPlateSetting SystemConfiguration::GetUnstackPlateCfg(const QString& id)
{
	return TransFormUnstackPlateCfg(m_cfg->GetUnstackPlateCfgByID(id));
}

/*获取拆垛托盘位置编号列表*/
QStringList SystemConfiguration::GetPathIds()
{
	return m_cfg->GetPathIDList();
}

/*根据托盘编号获取拆垛托盘配置*/
PathSetting SystemConfiguration::GetPathCfgById(const QString& id)
{
	return TransFromPathCfg(m_cfg->GetPathCfgById(id));
}


/*获取报警灯配置*/
AlarmSetting SystemConfiguration::GetAlarmLightSetting()
{
	return TransFromAlarmJ(m_cfg->GetAlarmLightCfg());
}

/*获取安全锁配置*/
SafeLockSetting SystemConfiguration::GetSafeLockerSetting()
{
	return TransFromSafeLockJ(m_cfg->GetSafeLockerCfg());
}
/*获取安全门配置*/
SafedoorSetting SystemConfiguration::GetSafeDoorSetting()
{
	return TransFromSafeDoorJ(m_cfg->GetSafeDoorCfg());
}

/*获取光栅配置*/
QList<GratingSetting> SystemConfiguration::GetGratingSetting()
{
	QList<GratingSetting> out;
	auto cfg = m_cfg->GetGratingCfg();
	for (auto& iter : cfg) {
		out.append(TransFromGratingJ(iter));
	}
	return out;
}
